/**
 * 622. 设计循环队列
 */

class MyCircularQueue {

    public int[] elem;
    int Front;//队头
    int Rear;//队尾
    public MyCircularQueue(int k) {
        elem = new int[k+1];
    }

    public boolean enQueue(int value) {
        if(isFull()){
            return false;
        }
        elem[Rear] = value;
        //Rear 往前走
        Rear = (Rear+1)%elem.length;//为了到最后一个元素后，可以走到0下标,环的灵魂
        return true;
    }

    public boolean deQueue() {
        if(isEmpty()){
            return false;
        }

        //Front 往前走。
        Front = (Front+1)%elem.length;
        return true;
    }

    public int Front() {
        if(isEmpty()){
            return -1;
        }
        return elem[Front];
    }

    public int Rear() {
        if(isEmpty()){
            return -1;
        }
        //如果rear走了一圈了，在0下标，那么应该取出7下标，length-1为了防止越界，否则rear-1就可以
        int index = Rear == 0 ? elem.length-1 : Rear-1;
        return elem[index];
    }

    public boolean isEmpty() {
        return Front == Rear;

    }

    public boolean isFull() {
        return (Rear+1)%elem.length == Front;
    }
}
